package com.fenda.servlet;

import com.fenda.Dao.UserDao;
import com.fenda.Util.mybatisUtil;
import com.fenda.pojo.Student;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.sendRedirect("add.html");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String hobby= req.getParameter("hobby");
        System.out.println(name+age+hobby);
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.add(new Student(id,name,age,hobby));
        sqlSession.commit();
        resp.sendRedirect("index.jsp");
        System.out.println("添加成功");
    }
}
